<template>
	<view class="page-index">
		<view class="bar-area">
			<view
				class="fc-white fs30"
				@click="loginOut"
				:style="showQuitBtn ? '' : 'visibility: hidden;'"
				>退出</view
			>
			<view class="flex-row" @click="areaChange">
				<view class="fc-white fs35">检验执行</view>
			</view>
			<view class="fc-white fs30">{{ loginUser && loginUser.YHXM }}</view>
		</view>
        
		<view class="patient-info" v-if="step != 0">
			<view class="item-title">
				<view class="item-brch">{{ nowPatient.brch }}</view>
				<view class="item-info">
					<view class="item-line"></view>
					<view class="item-body">
						<view class="item-body-top">
							<view class="fs30 fb">{{ nowPatient.brxm }}</view>
							<u--image
								class="ml10"
								width="20"
								height="20"
								:src="
									nowPatient.brxbDm == 1
										? '/static/man.svg'
										: '/static/female.svg'
								"
							></u--image>
							<view class="fs30 fc-sub ml10">{{
								nowPatient.brnl
							}}</view>
							<view class="fs30 fc-sub ml10">{{
								nowPatient.mzhm
							}}</view>
						</view>
					</view>
				</view>
			</view>
			<view class="patientInfo-zd"> 诊断：{{ nowPatient.brzs }} </view>
			<view class="filter-button" @click="handleExecuteFilter()">{{
				executeFilter == '5' ? "已采集" : "未采集"
			}}</view>
		</view>
		<view class="scan-tips-warning" v-if="step == 0">
			请扫描病人腕带进行确认
		</view>
		<view class="scan-tips-primary" v-if="step == 1">
			已确认病人腕带信息
		</view>
		<view class="scan-tips-warning" v-if="step == 2">
			请再确认患者腕带信息
		</view>

		<!-- <view style="display:flex;position:absolute;top:0rpx;left:100rpx;">
			<view>
				<view @click="scanTest(testBrid,1)">腕带扫描测试</view>
				<view @click="scanTest(textInspectId,2)">样品扫描测试</view>
			</view>
			<view>
				<input type="text" v-model="testBrid" placeholder="请输入病人id">
				<input type="text" v-model="textInspectId" placeholder="请输入检验号">
			</view>
		</view> -->

		<view class="main-context" v-if="step != 0">
			<scroll-view
				class="scroll-view"
				scroll-y="auto"
				:refresher-enabled="false"
				@refresherrefresh="onPullRefresh"
				@scrolltolower="onReachBottomDistance"
				:refresher-triggered="isRefresherTriggered"
			>
				<view>
					<view class="item-area" v-for="(item,index) in listData" :key="index">
						<view>
							<view class="item-content">
								<view class="item-content-icon">{{item.sampleTypeName}}</view>
								<view class="item-content-list">
									<view class="list-item">
										<view class="item-name">{{item.inspectItemName}}</view>
									</view>
								</view>
							</view>
							<view class="item-bottom">
								<view class="plan-datetime">
									{{item.barCodeNo}}
								</view>
								
							</view>
						</view>
						<view class="item-check-status" >
							<view :class="item.fgSampling == '1'?'item-check-no':
									item.fgSampling == '5'?'item-check-yes':'item-check-no'">
								<u--image width="16" height="16"
								:src="item.fgSampling == '1'?'/static/inspect/warning.svg':
								item.fgSampling == '5'?'/static/inspect/success.svg':''"></u--image>
								{{item.fgSampling == '1'?'未采集':
								item.fgSampling == '5'?'已采集':''}}
							</view>
						</view>
					</view>

					<u-empty
						:show="!(listData && listData.length > 0)"
						marginTop="300"
					></u-empty>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
import { mapState } from "vuex"
import { logout } from "@/api/utils.js"
import {
	getDateTimeString,
	getInspectType,
	getTimestamp,
	showToast,
} from "@/static/js/common.js"
// import plus from "uni-app-plus"

export default {
	components: {},
	props: {
		showQuitBtn: {
			type: Boolean,
			default: function () {
				return true
			},
		},
	},
	data() {
		return {
			step: 0, // 0:初始化空白状态  1:已扫描腕带  2:已扫描输液贴

			isRefresherTriggered: false,

			listData: [], // 医嘱计划处理后的数据
			nowPatient: {
				brch: "Q123", //
				brxm: "张三丰", //
				brxbDm: "1", //
				brnl: "12岁", //
				mzhm: "BLH112312313", //
				brzs: "感到不舒服", //
			}, // 当前病人
			nowIdVismed: "", //当前患者的就诊流程号
			nowIdMedord: "", //当前扫描出来的组号

			nowInspect: {}, //当前检验

			executeFilter: '1', // '1'：未采集 '5'：已采集

			testBrid: "659f803f4837db00155ee9a2", //
			textInspectId: "400010102181", //
		}
	},
	watch: {},
	computed: {
		...mapState(["loginUser", "area", "skinTestPatientList"]),
		actions() {
			if (this.area && this.area.length > 0) {
				let list = this.area.map((item) => {
					return {
						name: item.ksmc,
						code: item.ksdm,
					}
				})
				return list
			} else {
				return []
			}
		},
	},
	beforeDestroy() {
		uni.$off("scanCodeEmit") //取消监听
	},
	mounted() {
		// 接收原生扫描传值     监听扫描事件
		uni.$on("scanCodeEmit", async (res) => {
			console.log("检验执行扫描内容：", res)
			// uni.$emit("scanCodeEmit", res)
			if(res.length > 20){//是腕带
				//是腕带
				let patientRes = await this.getPatientByBrid(res)
				if (patientRes.res.Data.length == 0) {
					showToast(-1,'没有找到病人信息')
					return
				}
				this.executeFilter = '1';//查找未采集
				this.nowPatient = patientRes.res.Data[0] //获取病人信息
				await this.getInspectList();
				this.step = 1 //当前已扫描腕带
			}else{//是检验袋
				if (this.step != 1) {
					// 没有扫描腕带
					showToast(-1, "请先扫描患者腕带")
					return
				}
				// this.step = 2 //已扫描输液贴

				let inspectItem = this.listData.find(
					(el) => el.barCodeNo == res
				) //列表数据中查询检验

				if (this.executeFilter == '5') {//已采集列表
					if (inspectItem) {
						showToast(-1,'样本已采集')
						return
					} else {
						showToast(-1,'未找到检验信息')
						return
					}
				} else if (this.executeFilter == '1') {//未采集列表
					if (inspectItem) {
						this.nowInspect = inspectItem //暂存当前检验
						this.handleScanInspect();//执行扫描
					} else {
						showToast(-1,'未找到检验信息')
						return
					}
				}
			}
		})

		console.log("当前登陆人信息：", this.loginUser)
	},
	methods: {
		/**
		 * 扫描测试
		 * type 1 腕带   2 输液贴
		 */
		async scanTest(code, type) {
			if (type == 1) {
				//是腕带
				let patientRes = await this.getPatientByBrid(code)
				if (patientRes.res.Data.length == 0) {
					showToast(-1,'没有找到病人信息')
					return
				}
				this.executeFilter = '1';//查找未采集
				this.nowPatient = patientRes.res.Data[0] //获取病人信息
				await this.getInspectList();
				this.step = 1 //当前已扫描腕带
			} else if (type == 2) {
				if (this.step != 1) {
					// 没有扫描腕带
					showToast(-1, '请先扫描患者腕带')
					return
				}
				// this.step = 2 //已扫描输液贴

				let inspectItem = this.listData.find(
					(el) => el.barCodeNo == code
				) //列表数据中查询检验

				if (this.executeFilter == '5') {//已采集列表
					if (inspectItem) {
						showToast(-1, '样本已采集')
						return
					} else {
						showToast(-1, '未找到检验信息')
						return
					}
				} else if (this.executeFilter == '1') {//未采集列表
					if (inspectItem) {
						this.nowInspect = inspectItem //暂存当前检验
						this.handleScanInspect();//执行扫描
					} else {
						showToast(-1, '未找到检验信息')
						return
					}
				}
			}
		},

		/**
		 * 根据 就诊流程号 获取病人皮试信息
		 * mzhm 病人病案号
		 */
		getInspectList(mzhm) {
			this.listData = [] //置空列表
			let params = {
				cdVismed:this.nowPatient.hisJzxh,//就诊流水号
				fgSampling:this.executeFilter,// 采样状态: 1表示待采集状态, 5表示已采集状态
				inspectType: "2",// 2：检验物  1：血交叉
				orgCode:this.loginUser.JZJGID,//机构编码
				// end: "2024-01-11T01:53:24.472Z",
				// start: "2024-01-11T01:53:24.472Z",
			}

			return this.$commonAjax(
				'auth/mobile/advice/adviceinspect/scanSearchInspecBypatientId', 
				params
			).then((res) => {
				if(res.res.ReType == 0){
					console.log("检验列表", res)
					this.listData = res.res.Data;
					this.isRefresherTriggered = false;
				}else{
					this.showToast(res.res.ReType,res.res.Msg);
					this.isRefresherTriggered = false;
				}
			})
		},

		/**
		 * 点击批示提示框确认按钮
		 */
		confirmSkinTestTips() {
			this.skinCompleteTipsShow = false //关闭弹窗
		},

		/**
		 * 检验扫描
		 */
		handleScanInspect() {
			let params = {
				barCodeNo:this.nowInspect.barCodeNo,// 条码编号
				cdVismed:this.nowPatient.hisJzxh,// 病案号
				orgCode:this.loginUser.JZJGID.toString(),// 机构编码
				opertionType:'5',// 操作类型：5执行操作，0：撤销操作
				
				samplingOneUser:this.loginUser.YHXM,// 双人核对采样人1
				samplingOneDate:getDateTimeString(new Date()),// 双人核对采样人1的 采样日期，格式yyyy-MM-dd HH:mm:ss
				// samplingTwoUser:'',// 双人核对采样人2 
				// samplingTwoDate:'',// 双人核对采样人2的 采样日期，格式yyyy-MM-dd HH:mm:ss
			}
			console.log('入参：',params);
			return this.$commonAjax(
				"auth/mobile/advice/adviceinspect/scandoSampling",
				params
			).then((res) => {
				if (res.res.ReType == 0) {
					showToast(0, res.res.Msg) //消息提示
					setTimeout(()=>{
						this.executeFilter = '5';
						this.getInspectList();//刷新列表
					},1000);
				} else {
					showToast(-1, res.res.Msg) //消息提示
				}
			})
		},

		//过滤 已采集/未采集
		handleExecuteFilter() {
			this.executeFilter = this.executeFilter == '1' ? '5' : '1'
			this.getInspectList() //刷新数据
		},
		

		/**
		 * 根据病人id 获取病人详情
		 * brid 病人id
		 */
		getPatientByBrid(brid) {
			let params = {
				orgId: this.loginUser?.JGID, //长机构id
				// jzlx: null, // 就诊类型 1 抢救 2 留观 3诊室 不传就是全部
				lspb: 0, //离室判别
				yhdm: this.loginUser?.YHDM, // 当前登录用户的员工代码
				type: 1, //：病人列表   2：我的关注
				// hisJzxh: idVismed,
				// idMedord:idMedord,// 医嘱组号
				brid: brid, //病人id
			}
			return this.$commonAjax(
				"auth/mobile/patient/get/getPatientList",
				params
			)
		},

		loginOut() {
			logout()
		},
		onPullRefresh() {
			if (this.isRefresherTriggered) {
				return
			}
			this.isRefresherTriggered = true
			this.getInspectList()
		},
		onReachBottomDistance() {
			console.log(1111)
		},
	},
}
</script>

<style lang="scss">
.main-context {
	position: relative;
	height: calc(100% - 260rpx);
}
.scan-tips-warning {
	text-align: center;
	background-color: #faefe4;
	font-size: 30rpx;
	color: #ff9a27;
	padding: 10rpx 0rpx;
}
.scan-tips-primary {
	text-align: center;
	background-color: #4089fe1f;
	font-size: 30rpx;
	color: #4088fe;
	padding: 10rpx 0rpx;
}
.bar-area {
	height: 100rpx;
	background-color: $bs-color-primary;
	display: flex;
	flex-direction: row;
	align-items: center;
	font-size: 30rpx;
	justify-content: space-between;
	padding: 0 30rpx;
}
.scroll-view {
	width: 100%;
	height: calc(100% - 26rpx);
	background-color: #f3f5f7;
	padding-bottom: 20rpx;
}
.patient-info {
	padding: 20rpx;
	position: relative;

	.filter-button {
		font-size: 28rpx;
		color: #4088fe;
		background-color: #ebf3fe;
		border: 2rpx solid #4088fe;
		border-radius: 6rpx;
		width: 100rpx;
		text-align: center;
		padding: 10rpx 10rpx;
		position: absolute;
		right: 20rpx;
		top: 14rpx;
		z-index: 100;
	}
}
.item-area {
	position: relative;
	margin: 20rpx 20rpx 0 20rpx;
	background-color: #ffff;
	border-radius: 10rpx;
	padding: 10rpx;
	display: flex;
	.isExecute {
		position: absolute;
		right: -30rpx;
		top: 0rpx;
		width: 0rpx;
		height: 0rpx;
		border-right: 50rpx solid transparent;
		border-left: 50rpx solid transparent;
		border-bottom: 50rpx solid #26bf40;
		transform: rotate(45deg);
		.my-icon {
			position: relative;
			right: 10rpx;
			top: 24rpx;
			font-weight: bold;
			-webkit-transform: rotate(-45deg);
			transform: rotate(-45deg);
			color: #ffffff;
		}
	}
}
.item-title {
	padding: 6rpx 10rpx 12rpx 10rpx;
	display: flex;
}
.item-line {
	width: 1rpx;
	background-color: #d6d7d9;
	height: 46rpx;
	width: 2rpx;
}

.item-body {
	display: flex;
	flex-direction: column;
	justify-content: center;
	padding: 0 20rpx;
}
.item-body-top {
	display: flex;
	flex-direction: row;
	align-items: center;
	margin-bottom: 10rpx;
}
.item-check-status {
	font-size: 27rpx;
	margin-left: auto;
	margin-right: 10rpx;
	font-weight: bold;
	display: flex;
    align-items: center;
	.item-check-no{
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 8rpx 15rpx;
		border-radius: 8rpx;
		background-color: #fff5ea;
        margin-right: 10rpx;
		height: 40rpx;
	}
	.item-check-yes{
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 8rpx 15rpx;
		border-radius: 8rpx;
		background-color: #26bf4017;
        margin-right: 10rpx;
		height: 40rpx;
	}
}

.item-brch {
	width: 98rpx;
	font-size: 30rpx;
	color: #e06263;
	font-weight: bold;
}
.item-info {
	display: flex;
}

.item-content {
	font-size: 28rpx;
	padding: 10rpx 10rpx 4rpx 10rpx;
	display: flex;
	position: relative;
	.item-content-icon {
		font-size: 25rpx;
		padding: 4rpx 8rpx;
		border-radius: 8rpx;
		background-color: #fff5ea;
		color: #ff9a27;
		// width: 28rpx;
		height: 37rpx;
		margin-right: 10rpx;
	}
	.item-content-list {
		padding: 6rpx 8rpx;
		.list-item {
			display: flex;
			margin-bottom: 6rpx;
			max-width: 480rpx;
			.item-name {
				color: #262626;
				margin-right: 20rpx;
			}
			.item-amount {
				color: #262626;
				color: #4088fe;
			}
		}
	}
	.item-usage {
		background-color: #f0f3f6;
		border-radius: 8rpx;
		padding: 4rpx 16rpx;
		height: 35rpx;
		font-size: 25rpx;
		position: absolute;
		bottom: 15rpx;
		right: 10rpx;
	}
	.item-check-yes {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 8rpx 15rpx;
		border-radius: 8rpx;
		background-color: #26bf4017;
		margin-bottom: 10rpx;
		margin-left: auto;
	}
}
.check-datetime {
	font-size: 25rpx;
	color: #909399;
	padding: 0rpx 12rpx 8rpx 12rpx;
}
.item-bottom {
	display: flex;
	align-items: center;
	.plan-datetime {
		font-size: 25rpx;
		color: #909399;
		padding: 0rpx 12rpx 8rpx 12rpx;
	}
}

.patientInfo-zd {
	width: calc(100% - 100rpx);
	font-size: 25rpx;
	color: #909399;
	padding: 0rpx 12rpx 8rpx 12rpx;
	position: relative;
	left: 116rpx;
}
.double-check {
	height: 600rpx;
	position: relative;
	padding: 20rpx;
	.double-check-title {
		font-size: 34rpx;
		text-align: center;
		margin-bottom: 30rpx;
	}
	.double-check-form-item {
		display: flex;
		align-items: baseline;
		margin-bottom: 20rpx;
		.double-check-form-item-label {
			font-size: 32rpx;
			width: 200rpx;
		}
		.double-check-form-item-input {
			.u-input {
				background-color: #f9f9f9;
				width: 480rpx;
			}
		}
		.u-radio {
			margin-bottom: 40rpx !important;
		}
	}
	.submit-button {
		bottom: 10px;
		text-align: center;
		button {
			color: #ffffff;
			background-color: #4088fe;
			width: calc(100% - 20rpx);
			position: absolute;
			bottom: 0px;
		}
	}
}
</style>
